/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JProgressDialog.java
 *
 * Created on Oct 9, 2009, 11:23:43 AM
 */

package dasher.ui;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import org.jdesktop.swingworker.SwingWorker;

/**
 *
 * @author joshua
 */
public class JProgressDialog extends javax.swing.JDialog implements PropertyChangeListener {

    /** Creates new form JProgressDialog */
    public JProgressDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }
    
    public void show(String msg, int max) {
        setTitle(msg);
        setProgress(0, max);        
    }
    public void show(String msg) {
        show(msg, 100);
    }
    public void show(String msg, boolean indeterminate) {
        setTitle(msg);
        jProgressBar.setIndeterminate(true);
    }
    public void doTask(String msg, int max, SwingWorker worker) {
        setTitle(msg);
        setProgress(0, max);
        worker.addPropertyChangeListener(this);
        worker.execute();
    }

    /**
     * Sets the current progress
     *
     * @param done Amount done
     * @param max Maximum amount
     */
    public void setProgress(int done, int max) {
        jProgressBar.setIndeterminate(false);
        jProgressBar.setMaximum(max);
        jProgressBar.setValue(done);
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jProgressBar = new javax.swing.JProgressBar();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setAlwaysOnTop(true);
        setModal(true);
        setResizable(false);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jProgressBar, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 346, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jProgressBar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                JProgressDialog dialog = new JProgressDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JProgressBar jProgressBar;
    // End of variables declaration//GEN-END:variables

    public void propertyChange(PropertyChangeEvent evt) {
        if ("progress".equals(evt.getPropertyName())) {
            jProgressBar.setValue((Integer) evt.getNewValue());
        } else if ("state".equals(evt.getPropertyName())
                && ((SwingWorker.StateValue) evt.getNewValue()) == SwingWorker.StateValue.DONE) {
            setVisible(false);
        }
    }

}
